/**
 * @file models/app.js
 *  全局模型管理
 * @author maoquan(maoquan@htsc.com)
 */

import api from '../api';
import { routerRedux } from 'dva/router';

import { menu } from '../utils';

export default {
	namespace: 'app',
	state: {
		// 屏幕较小时侧栏以popover形式展现
		useMenuPopover: document.body.clientWidth < 769,
		// 屏幕较小时侧栏以popover形式展现，menuPopoverVisible控制popover是否显示
		menuPopoverVisible: false,
		// 侧栏是否显示
		siderFold: localStorage.getItem('htSiderFold') === 'true',
		// 深色 / 浅色主题切换
		darkTheme: localStorage.getItem('htDarkTheme') !== 'false',
		// 当前已展开侧栏菜单
		navOpenKeys: [],
	},
	subscriptions: {

		setup({ dispatch, history }) {
			window.addEventListener(
				'resize',
				() => {
					dispatch({ type: 'changeNavbar' });
				},
			);
			history.listen(({ pathname, query }) => {
				if( pathname === '/' ){
					dispatch(routerRedux.push('/welcomehtzq'))
				}
			})
		},
	},
	effects: {
		* switchSider({
			payload,
		}, { put }) {
			yield put({
				type: 'handleSwitchSider',
			});
		},
		* changeTheme({
			payload,
		}, { put }) {
			yield put({
				type: 'handleChangeTheme',
			});
		},
		* changeNavbar({
			payload,
		}, { put }) {
			if (document.body.clientWidth < 769) {
				yield put({ type: 'showNavbar' });
			} else {
				yield put({ type: 'hideNavbar' });
			}
		},
		* switchMenuPopover({
			payload,
		}, { put }) {
			yield put({
				type: 'handleSwitchMenuPopver',
			});
		},
		* changeRouter({
			payload
		}, {put}) {
			yield put(routerRedux.push(payload))
		},
		// * queryUser({
		// 	payload,
		// }, { call, put }) {
		// 	const data = yield call(api.queryUser, payload);
		// 	if (data.success && data.user) {
		// 		yield put({
		// 			type: 'querySuccess',
		// 			payload: data.user,
		// 		});
		// 		yield put(routerRedux.push('/'));
		// 	} else if (!location.href.indexOf('login') > -1) {
		// 		const urlHash = location.hash;
		// 		const from = urlHash.slice(1, urlHash.indexOf('?'));
		// 		window.location = `${location.origin}/#/login?from=${from}`;
		// 	}
		// },
	},
	reducers: {
		handleSwitchSider(state) {
			localStorage.setItem('htSiderFold', !state.siderFold);
			return {
				...state,
				siderFold: !state.siderFold,
			};
		},
		handleChangeTheme(state) {
			localStorage.setItem('htDarkTheme', !state.darkTheme);
			return {
				...state,
				darkTheme: !state.darkTheme,
			};
		},
		showNavbar(state) {
			return {
				...state,
				useMenuPopover: true,
			};
		},
		hideNavbar(state) {
			return {
				...state,
				useMenuPopover: false,
			};
		},
		handleSwitchMenuPopver(state) {
			return {
				...state,
				menuPopoverVisible: !state.menuPopoverVisible,
			};
		},
		changeOpenKeys(state, action) {
			return {
				...state,
				...action.payload,
			};
		},
	},
};
